<?php
/**
 *行业资讯
 * User: 江玉婵（QQ:1129901500)
 * Date: 2017/7/14
 * Time: 10:28
 */
namespace app\Index\controller;

use app\admin\model\News as News_mod;

class Hyzx extends Base
{

    //这个方法没执行
    public function _initialize()
    {
        //需要调用`Model`的`initialize`方法
        parent::_initialize();
        //TODO:自定义的初始化
        $this->mod = new News_mod();
        $this->hit();
    }

    public function index()
    {
        $cate_id = trim ($this->request->param('c_id',2));

        if ( $cate_id ) { $where['cate_id'] = $cate_id; }
        $where['t.is_work'] = 1;
        $field = "t.id,t.title,t.keyword,t.content,t.cate_id,t.utime,c.name cate_name";
        $order = ['t.id' => 'desc'];
        $join = [['news_cate c','c.id = t.cate_id','left']];

        $list = $this->mod->field($field)->where($where)->order($order)->alias('t')->join($join)->paginate(5);

        $this->assign ( ['list'=>$list, 'c_id'=>$cate_id]);
        unset($list);
        return view();
    }

    public function show(){
        $id = trim ($this->request->param('id',1));

        if ( $id ) { $where['t.id'] = $id; }
        $where['t.is_work'] = 1;
        $field = "t.id,t.title,t.keyword,t.content,t.cate_id,t.utime,c.name cate_name";
        $order = ['t.id' => 'desc'];
        $join = [['news_cate c','c.id = t.cate_id','left']];

        $row = $this->mod->field($field)->where($where)->order($order)->alias('t')->join($join)->find();
        $this->mod->where('id', $id)->setInc('hit');

        $this->assign ( ['row'=>$row, 'id'=>$id]);
        unset($row);
        return view();
    }

    public function hit() {
        $where['is_work'] = 1;
        $field = "id,title";
        $order = ['hit'=>'desc','id'=>'desc'];

        $hit = $this->mod->field($field)->where($where)->order($order)->limit(5)->select();

        $this->assign ( ['hit'=>$hit]);
    }

}
